﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.ControlLib;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu.childFrm
{
    public partial class FrmModify : Form
    {
        private string BindVarName;
        public FrmModify(string titleName,string varName,string currentValue)
        {
            InitializeComponent();

            this.lbl_TitleName.Text = titleName;
            this.lbl_CurrentValue.Text = currentValue;
            BindVarName = varName;

            this.txt_SetValue.Focus();
        }
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        #region 无边框拖动

        //窗体拖动事件
        private bool _isDragging = false;
        private Point _startPoint = new Point(0, 0);
        private readonly string varName;

        private void TopPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _isDragging = true;
                _startPoint = new Point(e.X, e.Y);
            }
        }

        private void TopPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point newPoint = this.PointToScreen(new Point(e.X, e.Y));
                this.Location = new Point(newPoint.X - _startPoint.X, newPoint.Y - _startPoint.Y);
            }
        }

        private void TopPanel_MouseUp(object sender, MouseEventArgs e)
        {
            _isDragging = false;
        }
        #endregion

        private void btn_Save_Click(object sender, EventArgs e)
        {
            var result =  CommonMethods.CommonWrite(this.BindVarName, this.txt_SetValue.Text);
            if (result.IsSuccess)
            {
                CommonMethods.AddOpLog(false, $"修改参数成功,参数名称：{this.lbl_TitleName.Text}" +
                    $",原值 ：{this.lbl_CurrentValue.Text}, 修改值：{this.txt_SetValue.Text}");
                this.Close();
            }
            else
            {
                CommonMethods.AddOpLog(false, $"修改参数失败,参数名称：{this.lbl_TitleName.Text}" +
                  $",{result.Message}");
                MessageBox.Show("修改参数失败" + result.Message);
            }
        }

        /// <summary>
        /// 数字键盘修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_SetValue_Click(object sender, EventArgs e)
        {
            if (this.Controls["NumKeyboard"] != null)
            {
                //置于最前面
                this.Controls["NumKeyboard"].BringToFront();
            }
            else
            {
                NumKeyboard numKeyboard = new NumKeyboard();

                //控制显示在窗体中间
                numKeyboard.Location = new Point((this.Width - numKeyboard.Width) / 2,
                    (this.Height - numKeyboard.Height) / 2);

                numKeyboard.Name = "NumKeyboard";
                numKeyboard.MaxValue = 100.0f;
                numKeyboard.MinValue = -100.0f;
                //设置默认值
                numKeyboard.InitialValue = this.lbl_CurrentValue.Text;
                //添加事件
                numKeyboard.ESCClick += NumKeyboard_ESCClick;
                numKeyboard.Enter += NumKeyboard_Enter;
                this.Controls.Add(numKeyboard);
                //置于最前面
                numKeyboard.BringToFront();
            }

        }

        private void NumKeyboard_Enter(object sender, EventArgs e)
        {
            if (sender is NumKeyboard numKeyboard)
            {
                this.txt_SetValue.Text = numKeyboard.ResultValue;
                this.Controls.Remove(numKeyboard);
            }
        }

        private void NumKeyboard_ESCClick(object sender, EventArgs e)
        {
            if (sender is NumKeyboard numKeyboard)
            {
                this.txt_SetValue.Text = numKeyboard.ResultValue;
                this.Controls.Remove(numKeyboard);
            }
        }

    }
}
